package com.xzx.study.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.xzx.study.R;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/4/14 20:42
 */
public class TitleFragment extends ListFragment {
    private static final String[] titles = {"title1", "title2", "title3", "title4", "title5"};
    private static final String[] details = {"detail1", "detail2", "detail3", "detail4", "detail5"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // 设置listView为单选模式
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        // 设置adapter， 显示数据
        setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.fragment_list_item, titles));
        // 设置第一个item被选中
        getListView().setItemChecked(0, true);
        switchItem(0);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        switchItem(position);
    }

    private void switchItem(int position) {
        Fragment fragment = new DetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("detail", details[position]);
        fragment.setArguments(bundle);
        getFragmentManager().beginTransaction().replace(R.id.fragment_container2, fragment).commit();
    }
}
